package tom.spring.cloud.provider.controller;


import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ZHUFEIFEI
 */
@Slf4j
@RestController
@RequestMapping("hello")
public class HelloController {

    @GetMapping("/{name}")
    public String hello(@PathVariable("name") String name) {
        return "hello, " + name;
    }

    @GetMapping("timeout/{name}")
    public String timeout(@PathVariable("name") String name) throws InterruptedException {
        //随机阻塞一段时间模拟接口超时
        Thread.sleep(RandomUtils.nextInt(300));
        return "hello, " + name;
    }

    @GetMapping("all")
    public List<String> helloAll(String names) {
        log.info("hello all param : {}", names);
        String[] nameArr = names.split(",");
        List<String> list = Lists.newArrayList(nameArr);
        return new ArrayList<>(list.stream().map((n)->"hello, "+ n).collect(Collectors.toList()));
    }

    @Value("${provider01.key:defaultValue}")
    private String fromConfigServer;

    @GetMapping("config/git")
    public String configServer() {
        return this.fromConfigServer;
    }
}
